import {
	addRegistration,
	validatorProjectNameByAdd
} from "@/views/wy-asset-management/registration/api/registration.js";
import registrationOcrAdd from "~/views/wy-asset-management/registration/add/ocr-add.vue";
import moment from "moment";
export default {
	components: {
		registrationOcrAdd: registrationOcrAdd
	},
	data() {
		/**
		 * async / await 同步
		 * 校验申请单位的唯一性
		 */
		let validatorProjectName = async (rule, value, callback) => {
			if (value == "") {
				callback(new Error("请输入项目名称"));
			}
			await validatorProjectNameByAdd(value).then(respData => {
				let result = respData.data.data;
				if (result == false) {
					callback(new Error("项目名称重复"));
				}
			});
		};

		return {
			//上传文件相关
			busId: "",
			modelType: "asset-registration",
			url: "/admin/sys-file/upLodeFileEdit",

			//弹出框 初始时隐藏
			dialogVisible: false,
			formObj: {
				//基本信息
				assetRegistrationId: "",
				projectName: "",
				registrationUnit: "",
				registerDate: "",
				constructionUnit: "",
				constructionAddress: "",
				approvalDocumentNo: "",
				constructionStartTime: "",
				completedTime: "",
				finalAccountsDocumentNo: "",
				assetOccupyingUnit: "",
				superiorCompetentUnit: "",
				assetUseUnit: "",
				fixedAssets: 0,
				currentAssets: 0,
				intangibleAssets: 0,
				deferredAssets: 0,
				otherAssets: 0,
				stayWriteOffExpenditure: 0,
				total: 0,
				remarks: "",
				filledBy: "",
				reviewer: "",
				personInCharge: "",
				//附件
				useCardPropertyList: [],
				propertyIdList: []
			},
			fileList: [],
			rules: {
				projectName: [
					{
						required: true,
						message: "请输入项目名称",
						trigger: "blur"
					},
					{ validator: validatorProjectName, trigger: "blur" }
				],
				registrationUnit: [
					{
						required: true,
						message: "请输入登记单位",
						trigger: "blur"
					}
				],
				registerDate: [
					{
						required: true,
						message: "请输入填表日期",
						trigger: "blur"
					}
				],
				constructionUnit: [
					{
						required: true,
						message: "请输入建设单位",
						trigger: "blur"
					}
				],
				constructionAddress: [
					{
						required: true,
						message: "请输入项目建设地址",
						trigger: "blur"
					}
				],
				approvalDocumentNo: [
					{
						required: true,
						message: "请输入设计立项文号",
						trigger: "blur"
					}
				],
				constructionStartTime: [
					{
						required: true,
						message: "请输入建设开始时间",
						trigger: "blur"
					}
				],
				completedTime: [
					{
						required: true,
						message: "请输入竣工时间",
						trigger: "blur"
					},
					{ validator: this.validateCompletionDate, trigger: "blur" }
				],
				finalAccountsDocumentNo: [
					{
						required: true,
						message: "请输入决算批复文号",
						trigger: "blur"
					}
				],
				assetOccupyingUnit: [
					{
						required: true,
						message: "请输入资产占用单位",
						trigger: "blur"
					}
				],
				superiorCompetentUnit: [
					{
						required: true,
						message: "请输入上级主管单位",
						trigger: "blur"
					}
				],
				assetUseUnit: [
					{
						required: true,
						message: "请输入资产使用单位",
						trigger: "blur"
					}
				],
				filledBy: [
					{ required: true, message: "请输入填报人", trigger: "blur" }
				],
				reviewer: [
					{ required: true, message: "请输入复核人", trigger: "blur" }
				],
				personInCharge: [
					{
						required: true,
						message: "请输入单位负责人",
						trigger: "blur"
					}
				],
				//金额校验
				fixedAssets: [
					{
						required: true,
						message: "请输入金额",
						trigger: "change"
					},
					{
						pattern: /^(([1-9]{1}\d*)|(0{1}))(\.\d{1,2})?$/,
						message: "请输入合法的金额数字，最多两位小数",
						trigger: "change"
					}
				],
				currentAssets: [
					{
						required: true,
						message: "请输入金额",
						trigger: "change"
					},
					{
						pattern: /^(\-)?(([1-9]{1}\d*)|(0{1}))(\.\d{1,2})?$/,
						message: "请输入合法的金额数字，最多两位小数",
						trigger: "blur"
					}
				],
				intangibleAssets: [
					{
						required: true,
						message: "请输入金额",
						trigger: "change"
					},
					{
						pattern: /^(([1-9]{1}\d*)|(0{1}))(\.\d{1,2})?$/,
						message: "请输入合法的金额数字，最多两位小数",
						trigger: "change"
					}
				],
				deferredAssets: [
					{
						required: true,
						message: "请输入金额",
						trigger: "change"
					},
					{
						pattern: /^(([1-9]{1}\d*)|(0{1}))(\.\d{1,2})?$/,
						message: "请输入合法的金额数字，最多两位小数",
						trigger: "change"
					}
				],
				otherAssets: [
					{
						required: true,
						message: "请输入金额",
						trigger: "change"
					},
					{
						pattern: /^(([1-9]{1}\d*)|(0{1}))(\.\d{1,2})?$/,
						message: "请输入合法的金额数字，最多两位小数",
						trigger: "change"
					}
				],
				stayWriteOffExpenditure: [
					{
						required: true,
						message: "请输入金额",
						trigger: "change"
					},
					{
						pattern: /^(([1-9]{1}\d*)|(0{1}))(\.\d{1,2})?$/,
						message: "请输入合法的金额数字，最多两位小数",
						trigger: "change"
					}
				],
				total: [
					{
						required: true,
						message: "请输入金额",
						trigger: "change"
					},
					{
						pattern: /^(\-)?(([1-9]{1}\d*)|(0{1}))(\.\d{1,2})?$/,
						message: "请输入合法的金额数字，最多两位小数",
						trigger: "change"
					}
				]
			}
		};
	},
	created() {
		this.calculationTotal();
		// //计算合计金额
		// this.formObj.total =
		//     parseFloat(this.formObj.fixedAssets) +
		//     parseFloat(this.formObj.currentAssets) +
		//     parseFloat(this.formObj.intangibleAssets) +
		//     parseFloat(this.formObj.deferredAssets) +
		//     parseFloat(this.formObj.otherAssets) +
		//     parseFloat(this.formObj.stayWriteOffExpenditure);
	},
	methods: {
		// 竣工日期校验
		validateCompletionDate(rule, value, callback) {
			if (!this.formObj.constructionStartTime) return callback();
			const startDate = moment(value, "YYYY-MM-DD HH:mm:ss");
			const endDate = moment(
				this.formObj.constructionStartTime,
				"YYYY-MM-DD HH:mm:ss"
			);
			let diffDay = startDate.diff(endDate, "seconds");
			if (diffDay < 1) {
				return callback(new Error("竣工日期必须大于建设开始日期"));
			}
			return callback();
		},
		/**
		 * 计算合计金额
		 */
		calculationTotal() {
			this.formObj.total =
				parseFloat(this.formObj.fixedAssets) +
				parseFloat(this.formObj.currentAssets) +
				parseFloat(this.formObj.intangibleAssets) +
				parseFloat(this.formObj.deferredAssets) +
				parseFloat(this.formObj.otherAssets) +
				parseFloat(this.formObj.stayWriteOffExpenditure);
		},
		// 上传成功
		onSuccess(val) {
			this.formObj.assetRegistrationId = val.data;
			addRegistration(this.formObj)
				.then(data => {
					this.$message({
						type: "success",
						message: "操作成功"
					});
					//操作完成 跳转页面
					this.$router.push("/asset-management/registration-list");
				})
				.catch(error => {
					this.$message({
						type: "error",
						message: "操作失败"
					});
				})
				.finally(() => (this.loading = false));
		},
		//保存
		sendData() {
			this.$refs["ruleForm"].validate(valid => {
				if (valid) {
					if (this.fileList.length)
						this.$refs.registrationAddFile.submit(this.modelType);
					else {
						addRegistration(this.formObj)
							.then(data => {
								this.$message({
									type: "success",
									message: "操作成功"
								});
								//操作完成 跳转页面
								this.$router.push("/asset-management/registration-list");
							})
							.catch(error => {
								this.$message({
									type: "error",
									message: "操作失败"
								});
							})
							.finally(() => (this.loading = false));
					}
				} else {
					return false;
				}
			});
		}
	}
};
